﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CNShapes;
using BobcatMultiTouch;
using BobcatUtility;
using System.Drawing;

namespace CNPageEditor
{
    /// <summary>
    /// This class manages an actin that lets you draw a freehand shape. It is derived from
    /// the Action base class so that it can work with the ActionManager for undo, redo etc.
    /// </summary>
    public class Action_DrawFreehandShape : Action
    {
        #region Action methods

        /// <summary>
        /// We set up this action.
        /// </summary>
        public override void initialize()
        {
            // We create a new shape in the page that we'll be managing...
            m_FreehandShape = new Shape_Freehand();
            m_Page.addShape(m_FreehandShape, getActionOrigin());
        }

		/// <summary>
		/// Called when the action becomes inactive.
		/// </summary>
		public override void deactivate()
		{
		}

		/// <summary>
        /// Returns the action description.
        /// </summary>
        public override string getDescription()
        {
            return "Freehand shape";
        }

        /// <summary>
        /// Undo is supported for this action.
        /// </summary>
        public override bool supportsUndo()
        {
            return true;
        }

        /// <summary>
        /// This action is repeatable.
        /// </summary>
        public override bool isRepeatable()
        {
            return true;
        }

        /// <summary>
        /// We remove the shape from the page.
        /// </summary>
        public override void undo()
        {
            m_Page.removeShape(m_FreehandShape.ID, getActionOrigin());
        }

        /// <summary>
        /// We add the shape to the page...
        /// </summary>
        public override void redo()
        {
            m_Page.addShape(m_FreehandShape, getActionOrigin());
        }

        /// <summary>
        /// Called when we get a touch-down event. This happens when you start to draw
        /// a new line in the shape.
        /// </summary>
        public override void onTouchDown(TouchArgs touchArgs)
        {
            // We set the pen width relative to the current zoom factor...
            float fPenWidth = m_FreehandShape.getPenWidth() / m_PageViewerControl.ZoomFactor;
            m_FreehandShape.setPenWidth(fPenWidth, getActionOrigin());

            // We translate the point into shape-coordinates, and add it to the shape...
            PointF shapePoint = m_PageViewerControl.clientPointToShapePoint(touchArgs.ClientPosition);
            m_FreehandShape.addPoint(shapePoint, getActionOrigin());
        }

        /// <summary>
        /// Called when we get a touch-move event.
        /// </summary>
        public override void onTouchMove(TouchArgs touchArgs)
        {
            // We add the new point to our shape...
            PointF shapePoint = m_PageViewerControl.clientPointToShapePoint(touchArgs.ClientPosition);
            m_FreehandShape.addPoint(shapePoint, getActionOrigin());
        }

        /// <summary>
        /// Called when we get a touch-up event.
        /// </summary>
        public override void onTouchUp(TouchArgs touchArgs)
        {
            m_FreehandShape.editCompleted(getActionOrigin());
            raiseActionCompletedEvent();
        }


        #endregion

        #region Private data

        private Shape_Freehand m_FreehandShape = null; // The shape we're managing.

        #endregion

    }
}
